package com.zx.server.api.Impl;

import com.zx.server.entity.IdentityEntity;
import com.zx.server.api.IdentityServer;
import com.zx.server.service.IdentityService;
import com.zx.server.util.ResponseModel;
import com.zx.server.util.ResultHelper;
import com.zx.server.util.ServiceCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @auther: zhangxin
 * @Description:
 */
@RestController
public class IdentityServerImpl implements IdentityServer {


    @Autowired
    IdentityService indentityService;

    @Override
    @RequestMapping("/addIdentity")
    public int addIdentity(@RequestBody IdentityEntity identityEntity) {
        int num = indentityService.addIdentity(identityEntity);
        return num;
    }

    @Override
    @RequestMapping("/updateIdentityById")
    public ResponseEntity<ResponseModel> updateIdentityById(@RequestBody IdentityEntity identity) {

        try {
            if (StringUtils.isBlank(identity.getRealName())){
                new ResponseEntity<ResponseModel>(
                        ResultHelper.createResult(ServiceCode.PARMAMETER_ERROR, "参数有误"), HttpStatus.OK);
            }

            System.out.println(identity.toString()+"...................");
            HashMap<String, Object> returnMap = new HashMap<>();
            indentityService.updateIdentity(identity);
            returnMap.put("return", "更新成功");
            return new ResponseEntity<ResponseModel>(ResultHelper.createOKResult(returnMap), HttpStatus.OK);
        }catch (Exception e) {
            return new ResponseEntity<ResponseModel>(
                    ResultHelper.createResult(ServiceCode.REQUEST_SERVER_FAIL, "服务器请求异常"), HttpStatus.OK);
        }
    }

    @Override
    @RequestMapping("/getIdentityList")
    public List<IdentityEntity> getIdentityList(@RequestParam(value = "key",required = false) String key,@RequestParam(value = "limit",required = false) Integer limit,@RequestParam(value  = "offset",required = false) Integer offset) {

        return indentityService.getIdentityList(key,limit,offset);
    }
}
